package com.acwing.partition3;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/2/11 16:21
 */
public class AC238银河英雄传说 {

    private static final int N = 30010;
    private static int[] parent = new int[N], size = new int[N], dist = new int[N];

    private static int n = 0, m = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        m = Integer.parseInt(reader.readLine());
        for (int i = 0; i < N; i++) {
            parent[i] = i;
            size[i] = 1;
            dist[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            String[] ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[1]), b = Integer.parseInt(ss[2]);
            char op = ss[0].charAt(0);
            int pa = find(a), pb = find(b);
            if (op == 'M') {
                dist[pa] = size[pb];
                size[pb] += size[pa];
                parent[pa] = pb;
            } else {
                if (pa != pb) writer.write("-1\n");
                else writer.write(Math.max(0, Math.abs(dist[a] - dist[b]) - 1) + "\n");
            }
        }
        writer.flush();
    }

    private static int find(int x) {
        if (x != parent[x]) {
            int root = find(parent[x]);
            dist[x] += dist[parent[x]];
            parent[x] = root;
        }
        return parent[x];
    }
}
